package com.hsh.pojo;

import java.util.Arrays;
import java.util.List;

/**
 * @author xrkhy
 * @date 2025/6/28 22:52
 * @description
 */
public class Page<T> {
    // 每页显示的条数
    private Integer pageSize;
    // 当前页码
    private Integer currentPage;
    // 数据总条数
    private Integer totalCount;
    // 起始索引 = (当前页码-1)*每页显示的条数
    private Integer startIndex;
    // 响应返回结果
    private List<T> resultList;
    // 条件查询
    private T condition;
    public Page(){  }
    public Page(int currentPage, int pageSize) {
        this.pageSize = pageSize;
        this.currentPage = currentPage;
    }
    /** ElementUI 的 pagination 分页组件
     * @param pageSize 每页显示的条数
     * @param currentPage 当前页码
     * @param totalCount 数据总条数
     * @param startIndex 起始索引 = (当前页码-1)*每页显示的条数
     * @param resultList 结果集合：用于条件查询
     * @param condition 查询条件
     */
    public Page(Integer pageSize, Integer currentPage, Integer totalCount, Integer startIndex, List<T> resultList, T condition) {
        this.pageSize = pageSize;
        this.currentPage = currentPage;
        this.totalCount = totalCount;
        this.startIndex = startIndex;
        this.resultList = resultList;
        this.condition = condition;
    }

    /**
     * 计算开始索引
     */
    public void setStartIndex() {
        this.startIndex = (this.currentPage - 1 ) * this.pageSize;
    }

    public void setConditionInit(T condition) {
        if (condition == null) {
            this.condition = null;
            return;
        }
        // 检查所有字段是否均为 null
        boolean allNull = Arrays.stream(condition.getClass().getDeclaredFields())
                .peek(f -> f.setAccessible(true))
                .allMatch(f -> {
                    try {
                        return f.get(condition) == null;
                    } catch (IllegalAccessException e) {
                        return true;
                    }
                });
        this.condition = allNull ? null : condition;
    }


    @Override
    public String toString() {
        return "Page{" +
                "pageSize=" + pageSize +
                ", currentPage=" + currentPage +
                ", totalCount=" + totalCount +
                ", startIndex=" + startIndex +
                ", resultList=" + resultList +
                ", condition=" + condition +
                '}';
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public Integer getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(Integer totalCount) {
        this.totalCount = totalCount;
    }

    public Integer getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(Integer startIndex) {
        this.startIndex = startIndex;
    }

    public List<T> getResultList() {
        return resultList;
    }

    public void setResultList(List<T> resultList) {
        this.resultList = resultList;
    }

    public T getCondition() {
        return condition;
    }

    public void setCondition(T condition) {
        this.condition = condition;

    }
}
